
from fastapi import HTTPException
from sqlmodel import asc, desc, select
from api.deps import SessionDep
from schemas.page_schema import PageRequestParams,PageResponse
from models.order_processed_model import OrderProcessed
from schemas.order_processed_schema import OrderProcessedCreate
async def find_order_processed_by_id(*, session: SessionDep, id: int):
    """通过id查询已处理订单详情"""
    if not id:
        raise ValueError("order_processed_id is required")
    stmt = select(OrderProcessed).where(OrderProcessed.id==id)
    result = await session.exec(stmt)
    db_order_processed = result.first()
    if db_order_processed is None:
        raise HTTPException(404,"订单不存在")
    return db_order_processed

async def find_order_processed_by_page(*, session: SessionDep,
                    page:PageRequestParams):
    order_by_stmt = None
    if page.order_type == "asc":
        order_by_stmt = asc(page.order_by)
    else:
        order_by_stmt = desc(page.order_by)
    stmt = select(OrderProcessed).offset(page.page)\
        .limit(page.limit)\
        .order_by(order_by_stmt)
    resutl = await session.exec(stmt)
    db_order_processed = resutl.all()
    
    if db_order_processed:
        await session.refresh(db_order_processed)
    print(db_order_processed)    
    # return PageResponse(data=db_order_processed)
    return []
    